package com.summer.common.lib.datasource;

import org.apache.derby.jdbc.EmbeddedDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

public class SummerDataSource {

    private String persistenceUnitName;

    private String derbyDatabaseName;

    public Map<String, Object> getDerbyProperties() {
        Map<String, Object> properties = new HashMap<>();
        properties.put("hibernate.dialect", "org.hibernate.dialect.DerbyDialect");
        properties.put("hibernate.id.new_generator_mappings", false);
        properties.put("hibernate.generateDdl", true);
        properties.put("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
        properties.put("hibernate.show_sql", true);
        properties.put("hibernate.hbm2ddl.auto", "create");
        properties.put("hibernate.connection.datasource", getDataSource());
        return properties;
    }

    public DataSource getDataSource() {
        EmbeddedDataSource dataSource = new EmbeddedDataSource();
        dataSource.setDatabaseName(derbyDatabaseName);
        dataSource.setCreateDatabase("create");
        return dataSource;
    }

    /**
     * JNDI 自动注入，调用set方法
     *
     * @return
     */
    public String getPersistenceUnitName() {
        return persistenceUnitName;
    }

    public void setPersistenceUnitName(String persistenceUnitName) {
        this.persistenceUnitName = persistenceUnitName;
    }

    public String getDerbyDatabaseName() {
        return derbyDatabaseName;
    }

    public void setDerbyDatabaseName(String derbyDatabaseName) {
        this.derbyDatabaseName = derbyDatabaseName;
    }
}
